#! /user/bin/python3
# -*- coding:UTF-8 -*-

from abaqus import *
import testUtils
testUtils.setBackwardCompatibility()
from abaqusConstants import *
print("该脚本求解悬臂梁在压力荷载作用下的建模,提交分析和后处理等各方面的操作\n,脚本开始运行了哦!!!")

# build model
myModel = mdb.Model(name = "Beam")

# creat new view to show model and anlysis results
myViewport = session.Viewport(name = "Cantilever Beam Example", origin = (20,20), width = 150, height = 120)

import part 
# draw sketch
mySketch = myModel.ConstrainedSketch(name = "beamProfile", sheetSize = 250)
mySketch.rectangle(point1 = (-100,10), point2 = (100,-10))
myBeam = myModel.Part(name = "Beam", dimensionality = THREE_D, type = DEFORMABLE_BODY)
myBeam.BaseSolidExtrude(sketch = mySketch, depth =25.0)

import material
# create material
mySteel = myModel.Material(name = "Steel")
elasticPorperties = (209.E3, 0.3)
mySteel.Elastic(table = (elasticPorperties,))

import section
mySection = myModel.HomogeneousSolidSection(name = "beamSection", material = "Steel", thickness = 1.0)
region = (myBeam.cells,)
myBeam.SectionAssignment(region = region, sectionName = "beamSection")

import assembly
myAssembly = myModel.rootAssembly
myInstance = myAssembly.Instance(name = "beamInstance", part = myBeam, dependent = OFF)

import step 
myModel.StaticStep(name = "beamLoad", previous = "Initial", timePeriod = 1.0, initialInc = 0.1, description = "Load the top of the beam.")

import load
endFaceCenter = (-100,0,12.5)
endFace = myInstance.faces.findAt((endFaceCenter,))

endRegion = (endFace,)
myModel.EncastreBC(name = "Fixed" , createStepName = "beamLoad" , region = endRegion)

topFaceCenter = (0,10,12.5)
topFace = myInstance.faces.findAt((topFaceCenter,))

topSurface = ((topFace,SIDE1),)
myModel.Pressure(name = "Pressure" , createStepName = "beamLoad", region = topSurface , magnitude = 0.5)

import mesh
region = (myInstance.cells,)
elemType = mesh.ElemType(elemCode = C3D8I , elemLibrary = STANDARD)
myAssembly.setElementType(regions = region , elemTypes = (elemType,))
myAssembly.seedPartInstance(regions = (myInstance,) , size = 10.0)
myAssembly.generateMesh(regions = (myInstance,))

# show beam model after meshing
myViewport.assemblyDisplay.setValues(mesh = ON)
myViewport.assemblyDisplay.meshOptions.setValues(meshTechnique = ON)
myViewport.setValues(displayedObject = myAssembly)

import job
jobName = "beam_tutorial"
myJob = mdb.Job(name = jobName , model = "Beam" , description = "Cantilever beam tutorials")
myJob.submit()
myJob.waitForCompletion()
print("恭喜,分析已完成!^o^!")

import visualization
myOdb = visualization.openOdb(path = jobName + ".odb")
myViewport.setValues(displayedObject = myOdb)
myViewport.odbDisplay.display.setValues(plotState = CONTOURS_ON_DEF)
myViewport.odbDisplay.commonOptions.setValues(renderStyle = FILLED)

# save Mises-contour picture as file with PNG format
session.printToFile(fileName = "Mises" , format = PNG , canvasObjects = (myViewport,))
print("文件已保存于工作目录下,请查看!!")